<?php
/**
 * Created by PhpStorm.
 * User: Fedax
 * Date: 2018/1/27
 * Time: 0:25
 */

namespace app\admin\model;


class AuthAccess extends ManageBase
{
    /**
     * 检查权限
     * @param $uid
     * @param $rule_val
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkRule($uid, $rule_val)
    {
        if (empty($uid)) {
            return false;
        }
        if (session('role_id') == 1) {
            return true;
        }
        //dump(session('role_id'));die;
        if (is_string($rule_val)) {
            if (strpos($rule_val, ',') !== false) {
                $rule_val = explode(',', $rule_val);
            } else {
                $rule_val = [$rule_val];
            }
        }
        $map = array();
        $map[] = ['role_id', "=", session('role_id')];
        $map[] = ['rule_name', "in", $rule_val];
        $rules =  $this
            ->name('auth_access')
            ->where($map)
            ->find();
        //dump($rules);die;
        if ($rules) {
            return true;
        }
        return false;
    }

    public function getRuleVals($role_id)
    {
        return $rule_ids = $this
            ->where('role_id', $role_id)
            ->column('rule_name');
    }
}